package com.btex.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.btex.app.R;
import com.btex.app.entity.CoinTradeHistoryInfo;
import com.btex.app.tools.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wcx on 16/3/20.
 */
public class TradeHistoryAdapter extends BaseAdapter{
    private Context cxt;
    private List<CoinTradeHistoryInfo> data = new ArrayList<>();
    private String TAG = "TradeHistoryAdapter";

    public TradeHistoryAdapter(Context cxt) {
        this.cxt = cxt;
    }

    @Override
    public int getCount() {
        return data==null?0:data.size();
    }

    public void addData(boolean clear, List<CoinTradeHistoryInfo> coinTradeHistoryInfos){
        if(clear){
            data.clear();
            LogUtil.i(TAG, "刷新数据");
            LogUtil.i(TAG,"coinTradeHistoryInfos:"+coinTradeHistoryInfos.toString());
        }
        data.addAll(coinTradeHistoryInfos);
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int position) {
        return (data==null||data.size()==0)?null : (data.size()>=position?data.get(position):null);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(cxt).inflate(
                    R.layout.item_trade_history, null);
            holder = new ViewHolder();
            holder.tvBuySell = (TextView) convertView.findViewById(R.id.tv_buy_sell);
            holder.tvCtime = (TextView) convertView.findViewById(R.id.tv_ctime);
            holder.tvPrice = (TextView) convertView.findViewById(R.id.tv_price);
            holder.tvTradeNum = (TextView) convertView.findViewById(R.id.tv_trade_num);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        CoinTradeHistoryInfo coinTradeHistoryInfo = (CoinTradeHistoryInfo) getItem(position);
        if(coinTradeHistoryInfo!=null) {
            holder.tvCtime.setText(coinTradeHistoryInfo.getTime());
            holder.tvPrice.setText(coinTradeHistoryInfo.getPrice());
            holder.tvTradeNum.setText(coinTradeHistoryInfo.getTotal());
            if("sell".equals(coinTradeHistoryInfo.getTrade_info())){
                holder.tvBuySell.setText(cxt.getString(R.string.corn_sell));
                holder.tvBuySell.setTextColor(cxt.getResources().getColor(R.color.tv_green));
            }else{
                holder.tvBuySell.setText(cxt.getString(R.string.corn_buy));
                holder.tvBuySell.setTextColor(cxt.getResources().getColor(R.color.tv_red));
            }
        }

        return convertView;
    }

    private static class ViewHolder{
        private TextView tvCtime;
        private TextView tvBuySell;
        private TextView tvPrice;
        private TextView tvTradeNum;
    }

}
